package com.app.twth.ui.tally.sku;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.app.twth.app.Constant;
import com.app.twth.base.BaseAndroidViewModel;
import com.app.twth.bean.BaseResponseEntity;
import com.app.twth.bean.LoadingBean;
import com.app.twth.net.JsonCallBack;
import com.app.twth.ui.checkin.FileUpload;
import com.app.twth.ui.tally.TallyDetails;
import com.app.twth.ui.tally.TallySkuDetails;
import com.app.twth.utils.DateUtil;
import com.lzy.okgo.model.Response;

import java.util.List;


/**
 * ****************************************************************
 * 文件名称: LoginViewModel
 * 文件描述:
 * 注意事项:
 * 版权声明:
 * ****************************************************************
 */
public class TallySkuViewModel extends BaseAndroidViewModel<TallySkuDataModel> {

    public MutableLiveData<List<TallyDetails>> tallyDetailsLiveData = new MutableLiveData<>();
    public MutableLiveData<Integer> tallySkuDetailsLiveData = new MutableLiveData<>();
    public MutableLiveData<SkuInfo> tallySkuInfoLiveData = new MutableLiveData<>();
    public MutableLiveData<CheckSkuUnit> checkSkuUnitLiveData = new MutableLiveData<>();
    public MutableLiveData<FileUpload> fileUploadMutableLiveData = new MutableLiveData<>();


    public TallySkuViewModel(@NonNull Application application) {
        super(application);
    }

    public void getTallyDetail(int id) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.getTallyDetail(id, new JsonCallBack<BaseResponseEntity<List<TallyDetails>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<TallyDetails>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<TallyDetails>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    tallyDetailsLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void getTallySkuDetail(int position, int id) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.getTallySkuDetail(id, new JsonCallBack<BaseResponseEntity<List<TallySkuDetails>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<TallySkuDetails>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<TallySkuDetails>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    List<TallySkuDetails> data = baseResponseEntity.data;
                    if (data == null || data.size() == 0) {
                        return;
                    }
                    List<TallyDetails> tallyDetailList = tallyDetailsLiveData.getValue();
                    if (tallyDetailList == null || tallyDetailList.size() == 0 || tallyDetailList.size() < position) {
                        return;
                    }

                    for (int i = 0; i < tallyDetailList.size(); i++) {
                        TallyDetails tallyDetail = tallyDetailList.get(i);
                        if (position == i) {//设置显示并添加数据
                            tallyDetail.setClick(true);
                            tallyDetail.setLpnsList(data);
                        }
                    }
                    tallySkuDetailsLiveData.setValue(position);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void getTallySkuInfo(SkuInfoQr skuInfoQr, boolean isSubmit) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.getTallySkuInfo(skuInfoQr, new JsonCallBack<BaseResponseEntity<SkuInfo>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<SkuInfo>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<SkuInfo> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    SkuInfo data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    data.setSubmit(isSubmit);
                    tallySkuInfoLiveData.setValue(data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void checkSkuUnit(CheckSkuUnit skuInfoQr) {
        isLoading.setValue(new LoadingBean(true, Constant.load_check));
        errorResultCode.setValue("");
        mModel.tallyCheckSkuUnit(skuInfoQr, new JsonCallBack<BaseResponseEntity<CheckSkuUnit>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<CheckSkuUnit>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<CheckSkuUnit> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    CheckSkuUnit data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }

                    checkSkuUnitLiveData.setValue(data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void fileUpload(String containerNo, String fileName, String path, int type, boolean isSubmit, boolean showDialog) {
        isLoading.setValue(new LoadingBean(showDialog, Constant.loading));
        errorResultCode.setValue("");
        String name = "tally/" + containerNo + "-" + fileName + "-" + DateUtil.getYMD() + "-" + System.currentTimeMillis() + ".jpg";
        mModel.upload(name, path, new JsonCallBack<BaseResponseEntity<String>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<String>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<String> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    FileUpload fileUpload = new FileUpload();
                    fileUpload.setType(type);
                    fileUpload.setPath(baseResponseEntity.data);
                    fileUpload.setSubmit(isSubmit);
                    fileUploadMutableLiveData.setValue(fileUpload);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
                errorResultCode.setValue(baseResponseEntity.getCode());
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(error);
            }
        });
    }

}
